<?php
class Shopping_AdminCategoryController extends Zendvn_Controller_Action{
	
	protected  $_arrParam;
	protected  $_currentController;
	protected  $_actionMain;
	protected  $_namespace;
		
	public function init(){
		$this->_arrParam 			= $this->_request->getParams();
		
		$this->_currentController 	= '/'    . $this->_arrParam['module']
								     .'/'  . $this->_arrParam['controller'];
		
		$this->_actionMain 		   = '/'    . $this->_arrParam['module']
								     .'/'  . $this->_arrParam['controller']
								     .'/index';
		
								     
		//Tao namspace cho session
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->keywords)){
			$ssFilter->keywords = '';
		}
		$this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		
		
		//Truyen ra ngoai view
		$this->view->arrParam 			= $this->_arrParam;
		$this->view->currentController 	= $this->_currentController;
		$this->view->actionMain  		= $this->_actionMain;
		$this->view->namespace			= $this->_namespace;
		//Khai bao template		
		$template_path = TEMPLATE_PATH . "/admin/system";
		$this->loadTemplate($template_path,'template.ini','template');
	}	
	public function indexAction(){	
			
		$this->view->Title = 'Sản phẩm :: Quản lý danh mục :: Danh sách';
		$this->view->headTitle($this->view->Title,true);
		$tblCategory = new Shopping_Model_Category();
		$this->view->Items = $tblCategory->listItem($this->_arrParam,array('task'=>'admin-list'));
		
}
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if($this->_arrParam['type'] == 'search'){
			if($this->_arrParam['key']== 1){
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			}else{
				$ssFilter->keywords = '';
			}
		}
		
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function addAction(){
		$this->view->Title = 'Sản phẩm :: Quản lý danh mục :: Thêm mới';
		$this->view->headTitle($this->view->Title,true);
		
		$tblCategory = new Shopping_Model_Category();
		$this->view->SelectCategory = $tblCategory->itemInSelectbox();
		if($this->_request->isPost()){
			$validator = new Shopping_Form_Validate_ProductCategory_Addnew($this->_arrParam);
			if($validator->isError() == true){
				$this->view->arrErrors = $validator->getMessageError();
				$this->view->Item	   = $validator->getData();
			}else{
				$arrParam = $validator->getData();
				$tblCategory->saveItem($arrParam,array('task'=>'admin-add'));
				$this->_redirect($this->_actionMain);
			}
		}
	}
	public function infoAction(){
		$this->view->Title = 'Product :: Category manager :: Infomation';
		$this->view->headTitle($this->view->Title,true);
		$tblCategory = new Shopping_Model_Category();
		$this->view->SelectCategory = $tblCategory->itemInSelectbox();
		$this->view->Item = $tblCategory->getItem($this->_arrParam,array('task'=>'admin-info'));
	}
	
	public function editAction(){
		$this->view->Title = 'Sản phẩm :: Quản lý danh mục :: Sửa';
		$this->view->headTitle($this->view->Title,true);
		$tblCategory 				= new Shopping_Model_Category();
		$this->view->SelectCategory = $tblCategory->itemInSelectbox($this->_arrParam,array('task'=>'admin-edit'));
		$this->view->Item 		    = $tblCategory->getItem($this->_arrParam,array('task'=>'admin-edit'));
				
		if($this->_request->isPost()){
			$validator = new Shopping_Form_Validate_ProductCategory_Edit($this->_arrParam);
			if($validator->isError() == true){
				$this->view->arrErrors  = $validator->getMessageError();
				$newArr	   				= $validator->getData();
				$this->view->Item       = $newArr;
			}else{
				$tblCategory = new Shopping_Model_Category();
				$arrParams = $validator->getData('upload');
				$tblCategory->saveItem($arrParams,array('task'=>'admin-edit'));
				$this->_redirect($this->_actionMain);
			}
		}
		
	}
	public function deleteAction(){
		$this->view->Title = 'Sản phẩm :: Quản lý danh mục :: Xóa';
		$this->view->headTitle($this->view->Title,true);
		
		$this->view->Item = $this->_arrParam;
		if($this->_request->isPost()){
			$tblCategory = new Shopping_Model_Category();
			$tblCategory->deleteItem($this->_arrParam,array('task'=>'admin-delete'));
			$this->_redirect($this->_actionMain);
		}
	}
	public function statusAction(){
			$tblCategory = new Shopping_Model_Category();
			$tblCategory->changeStatus($this->_arrParam,array('task'=>'admin-status'));
			$this->_redirect($this->_actionMain);
			$this->_helper->viewRenderer->setNoRender();
	}
	
	public function multiDeleteAction(){
		if($this->_request->isPost()){
			$tblCategory = new Shopping_Model_Category();
			$tblCategory->deleteItem($this->_arrParam,array('task'=>'admin-multidelete'));
			$this->_redirect($this->_actionMain);
			
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	public function sortAction(){
		if($this->_request->isPost()){
			$tblCategory = new Shopping_Model_Category();
			$tblCategory->sortItem($this->_arrParam);
			$this->_redirect($this->_actionMain);
		}
		$this->_helper->viewRenderer->setNoRender();
	}
}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	